﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
namespace syc.Sale
{
    public partial class 原料采购计划录入 : System.Web.UI.Page
    {
        double[] chanliang = new double[5];
        protected HSSFWorkbook hssfworkbook;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                this.NianDu.Text = DateTime.Today.Year.ToString();
                this.YueFen.Text = DateTime.Today.Month.ToString();
                string y = Request.QueryString["y"];
                string m = Request.QueryString["m"];

                if (y == null || m == null || this.NianDu.Items.FindByValue(y) == null || this.YueFen.Items.FindByValue(m) == null)
                {

                }
                else
                {
                    int year = int.Parse(y);
                    int month = int.Parse(m);
                    this.NianDu.Text = y;
                    this.YueFen.Text = m;

                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;

                    Cmd.CommandText = "select * from 原料采购计划 where 年度=" + y + " and 月份=" + m;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.YouGuan.Text = ((double)Reader["油管"]).ToString();
                        this.TaoGuan.Text = ((double)Reader["套管"]).ToString();
                        this.YouGuanJiHuaZhiJinDanJia.Text = string.Format("{0:0.00}", Reader["油管单价"]);
                        this.TaoGuanJiHuaZhiJinDanJia.Text = string.Format("{0:0.00}", Reader["套管单价"]);
                        this.YouGuanJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJiHuaZhiJinDanJia.Text)*(double)Reader["油管"]));
                        this.TaoGuanJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJiHuaZhiJinDanJia.Text) * (double)Reader["套管"]));
                        this.NianDu.Enabled = false;
                        this.YueFen.Enabled = false;
                    }
                    Reader.Close();

                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();


                }
            }
        }
        private bool CheckInput()
        {
            if (!double.TryParse(this.YouGuan.Text, out chanliang[0]))
            {
                this.Message.Text = "·原料油管 计划产量录入不正确";
                return false;
            }
            if (!double.TryParse(this.TaoGuan.Text, out chanliang[1]))
            {
                this.Message.Text = "·原料套管 计划产量录入不正确";
                return false;
            }
            if (!double.TryParse(this.YouGuanJiHuaZhiJinDanJia.Text, out chanliang[2]))
            {
                this.Message.Text = "·原料油管 油管单价安排录入不正确";
                return false;
            }
            if (!double.TryParse(this.TaoGuanJiHuaZhiJinDanJia.Text, out chanliang[3]))
            {
                this.Message.Text = "·原料套管 套管单价安排录入不正确";
                return false;
            }
            return true;
        }
        protected void InitializeWorkbook()
        {

            FileStream file = new FileStream(HttpContext.Current.Server.MapPath("模板/原料采购计划.xls"), FileMode.Open, FileAccess.Read);

            hssfworkbook = new HSSFWorkbook(file);

            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "陕西延长石油材料有限公司";
            hssfworkbook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "报表";
            si.Title = "原料采购计划";
            hssfworkbook.SummaryInformation = si;
        }
        protected void WriteToFile()
        {
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + Server.UrlEncode("集体生产计划") + ".xls\"");
            hssfworkbook.Write(Response.OutputStream);
        }
        protected void Button_Save_Click(object sender, EventArgs e)
        {

            if (!CheckInput())
            {
                return;
            }
            
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;

            if (this.NianDu.Enabled)
            {

                Cmd.CommandText =
"if EXISTS(select * from 原料采购计划 where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + ")\n"
+ "begin\n"
+ "return\n"
+ "end\n"
+ "else\n"
+ "begin\n"
+ "insert into 原料采购计划(年度,月份,油管,套管,油管单价,套管单价,油管金额,套管金额)"
+ "values(" + this.NianDu.Text + "," + this.YueFen.Text
+ "," + chanliang[0]
+ "," + chanliang[1]
+ "," + chanliang[2]
+ "," + chanliang[3]
+ "," + (chanliang[0]*chanliang[2])
+ "," + (chanliang[1]*chanliang[3])
+ ")\n"
+ "end";
            }
            else
            {
                Cmd.CommandText =
"update 原料采购计划 set 油管=" + chanliang[0]
+ ",套管=" + chanliang[1]
+ ",油管单价=" + chanliang[2]
+ ",套管单价=" + chanliang[3]
+ ",油管金额=" + (chanliang[0] * chanliang[2])
+ ",套管金额=" + (chanliang[1] * chanliang[3])
+ " where 年度="
+ this.NianDu.Text + " and 月份=" + this.YueFen.Text;
            }


            if (Cmd.ExecuteNonQuery() < 1)
            {
                if (this.NianDu.Enabled)
                {
                    this.Message.Text = "·保存失败，计划已经存在";
                }
                else
                {
                    this.Message.Text = "·保存失败，您正在修改的计划可能已经被删除";
                }
            }
            else
            {
                this.Message.Text = "·保存成功";
                this.NianDu.Enabled = false;
                this.YueFen.Enabled = false;
            }

            Conn.Close();
            Cmd.Dispose();
            Conn.Dispose();

        }
      
        protected void Button_Calc_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                this.YouGuanJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJiHuaZhiJinDanJia.Text) * (double.Parse(this.YouGuan.Text))));
                this.TaoGuanJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJiHuaZhiJinDanJia.Text) * (double.Parse(this.TaoGuan.Text))));
            }
        }
    }
}
